//
// Created by wwwsp on 5/2/2022.
//

#include "Char_queue.h"

Char_queue::Char_queue() {
    head = new Node();
    head->next = nullptr;
    tail = nullptr;
    length = 0;
}

bool Char_queue::isEmpty() const {
    return (length == 0);
}

bool Char_queue::isFull() const {
    return (length == MAX_SIZE);
}

char Char_queue::pool() {
    if(isEmpty()) return '\0';
    char rs = '0';
    if(length == 1){
        rs = head->next->data;
        delete head->next;
        tail = nullptr;
        return rs;
    }
    Node* goer = head->next;
    rs = goer->data;
    head->next = head->next->next;
    delete goer;
    --length;
    return rs;
}

void Char_queue::post(char data) {
    if(isFull()) return;
    Node* newNode = new Node();
    newNode->data = data;
    newNode->next = nullptr;
    if(isEmpty()){
        head->next = newNode;
        tail = newNode;
        ++length;
        return;
    }
    Node* moveNode = head->next;
    while(moveNode->next){
        moveNode = moveNode->next;
    }
    moveNode->next = newNode;
    tail = newNode;
    ++length;
}

Char_queue::~Char_queue() {

}

void Char_queue::print() {
    if(isEmpty()) return;
    Node* moveNode = head->next;
    while(moveNode){
        std::cout << moveNode->data << " ";
        moveNode = moveNode->next;
    }
}


